#include "threadsafe_queue.h"
#include <iostream>
#include <thread>
#include <vector>

using std::cout;
using std::thread;
using std::vector;


void test()
{
    threadsafe_queue<int> q;
    vector<thread> threads;
    int hw_thread_num = thread::hardware_concurrency();
    // size_t thread_num = hw_thread_num > 0 ? (size_t)hw_thread_num : 8;
    size_t thread_num = 8;
    q.push(-1);
    // q.push(-1);

    cout << "create " << thread_num << " thread..\n";
    for (size_t i = thread_num; i > 0; --i)
    {
        if (i < thread_num / 2)
        {
            threads.emplace_back(thread([i, &q]
                                        {
                q.push(i);
                cout << "Thread>>" << std::this_thread::get_id() << " push " << i << "\n"; }));
        }
        else
        {
            threads.emplace_back(thread([&q]
                                        {
                shared_ptr<int> res = q.wait_and_pop();
                cout << "Thread>>" << std::this_thread::get_id() << " pop" << *res << "\n"; }));
        }
    }

    for (auto &th : threads)
    {
        if (th.joinable())
        {
            th.join();
        }
    }
    if(q.empty()) cout << "empty\n";
}

int main()
{
    test();
}